/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/6/13
 * \* Time: 19:16
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 爱吃香蕉的珂珂 {
    public int minEatingSpeed(int[] piles, int h) {
        int left=1;
        int right=getMax(piles);
        while (left<right){
            int mid=left+(right-left)/2;
            if(canFinish(piles,mid,h)){
                right=mid;
            }else if(!canFinish(piles,mid,h)){
                left=mid+1;
            }
        }
        return left;
    }

    int getMax(int[] piles){
        int max=-1;
        for(int n:piles){
            if(n>max){
                max=n;
            }
        }
        return max;
    }

    boolean canFinish(int[] piles,int speed,int h){
        int time=0;
        for(int i:piles){
            time+=timeOf(speed,i);
        }
        return time<=h;
    }
    int timeOf(int speed,int pile){
        return pile/speed+((pile%speed>0)?1:0);
    }
}